from flask import render_template, request, redirect, url_for, flash
from app.forms.auth import LoginForm
from app.model.user import User
from flask_login import login_user, login_required
from . import web

@web.route('/login', methods=['GET', 'POST'])
def login():
    form = LoginForm(request.form)
    if request.method == 'POST' and form.validate():
        user = User.query.filter_by(email=form.email.data).first()
        if user and user.check_password(form.password.data):
            login_user(user, remember=True)
            next = request.args.get('next')
            if not next:
                next = url_for('web.app_0')
            return redirect(next)

        else:
            flash('账号不存在或密码错误')

    return render_template('login.html', form=form)

# @web.route('/')
# @login_required
# def index():
#     return "<h1>YumChina 数据</h1>  <br> <a href='/one'>各城市门店数量</a> <br> " \
#            "<a href='/two'>所有门店分布数据</a>"
